package com.kitty.game.activity.service.seal;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.NPC;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.EquipBox;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.npc.model.NpcButton;
import com.kitty.game.player.PlayerService;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.NotifyModules;
import com.kitty.mina.message.MessagePusher;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**烟花年兽处理类*/
@Component
public class  YhuaNianshouBossHandler extends SealBossHandler {
    private final String ownerNpcName = "{0}的逃跑的年兽";
    private final String npcName = "逃跑的年兽";
    private final String bossName = "逃跑的年兽";
    private final List<String> smallBossNames = Arrays.asList("逃跑的年兽1");
    private final String smallName = "小妖";
    private final String fightTag = NpcButton.FIGHT_TO_NIANSHOU.getKey();
    private int firstTalkId = 1228;
@Autowired
    EquipService equipService;
    @Override
    public String getOwnerNpcName() {
        return ownerNpcName;
    }

    @Override
    public String getNpcName() {
        return npcName;
    }

    @Override
    public String getBossName() {
        return bossName;
    }

    @Override
    public List<String> getSmallBossNames() {
        return smallBossNames;
    }

    @Override
    public String getSmallName() {
        return smallName;
    }

    @Override
    public String getFightTag() {
        return fightTag;
    }

    @Override
    public int getFirstTalkId() {
        return firstTalkId;
    }

    @Override
    public byte getNpcType() {
        return NPC.TYPE_BIANYI_YHNS;
    }

    @Override
    protected int getFightType(Role role) {
        return Const.fightType_yanhuan_nianshou;
    }

    @Autowired
    PlayerService playerService;

    public void useYanHua(Role role, byte type, RoleEquip roleEquip) {
        if (role.getPos().getMapId() != 9000) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("烟花筒只能在北海沙滩使用！！"));
            return;
        }
        int teamSize = teamService.getTeamCount(role);
        if (teamSize < 1) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("请组队！"));
            return;
        }
        if(!teamService.isTeamLeader(role)){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("还是老老实实听队长指挥吧！"));
        }
//        if (equipService.delEquipNum(role, roleEquip, 1) <= 0) {
//            MessagePusher.pushMessage(role, new RespNotifyMiscEx(role.getName() + "#n没有挑战次数！"));
//            return;
//        }

        Team team = teamService.getTeam(role.getRoleId());//当前队伍
        for (Member member : team.getList()) { //先判断提示
            long uid = playerService.getUidBy(member.getRoleId());
            Role roles = playerService.getPlayerBy(uid);
            //先查询所有人是否有
            RoleEquip roleEquips = SpringUtils.getBean(EquipService.class).getRoleEquipByName(roles, roleEquip.getName());

            List<RoleEquip> list = new ArrayList<RoleEquip>();//查询背包中是否包含队长使用的道具
            EquipBox equipBox = roles.getEquipBox();//遍历所有道具
            Map<Integer, RoleEquip> equipBoxMap =  equipBox.getEquips();
            for (Map.Entry<Integer, RoleEquip> c : equipBoxMap.entrySet()) {
                RoleEquip re = c.getValue();
                if (re.getPosition() > 40 && re.getPosition() < 166) {//判断位置
                    if (re.getName().equals(roleEquip.getName())){//判断道具名称
                        list.add(re);
                    }
                }
            }
            if (list.size() == 0){//不存在直接提示返回
                for (Member member1 : team.getList()) {
                    Role roleAll = playerService.getPlayerBy(playerService.getUidBy(member1.getRoleId()));
                    MessagePusher.pushMessage(roleAll, new RespNotifyMiscEx(member.getName() + "#n背包没有"+roleEquip.getName() + "#n了"));
                }
                return;
            }

//            if (roleEquips.getPosition() > 40 && roleEquips.getPosition() < 166) {
//                if (!roleEquips.getName().equals(roleEquip.getName())) {
//                    for (Member member1 : team.getList()) {
//                        Role roleAll = playerService.getPlayerBy(playerService.getUidBy(member1.getRoleId()));
//                        MessagePusher.pushMessage(roleAll, new RespNotifyMiscEx(member.getName() + "#n背包没有"+roleEquip.getName() + "#n了"));
//                    }
//                    return;
//                }
//            }else {
//                for (Member member1 : team.getList()) {
//                    Role roleAll = playerService.getPlayerBy(playerService.getUidBy(member1.getRoleId()));
//                    MessagePusher.pushMessage(roleAll, new RespNotifyMiscEx(member.getName() + "#n背包没有"+roleEquip.getName() + "#n了"));
//                }
//                return;
//            }


//            if (StringUtils.isEmpty(roleEquips.getName())){
//                for (Member member1 : team.getList()) {
//                    Role roleAll = playerService.getPlayerBy(playerService.getUidBy(member1.getRoleId()));
//                    MessagePusher.pushMessage(roleAll, new RespNotifyMiscEx(member.getName() + "#n背包没有"+roleEquip.getName() + "#n了"));
//                }
//                return;
//            }
        }

        //正常扣除
        for (Member member : team.getList()) {
            long uid = playerService.getUidBy(member.getRoleId());
            Role roles = playerService.getPlayerBy(uid);
            //扣物品，不知道为什么返回数没用。
            SpringUtils.getEquipService().delRoloEquip(roleEquip.getName(), 1, true, roles);
        }

        //关闭背包
        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_CLOSE_DLG); //关闭对话框
        respGeneralNotify.setValue("BagDlg");
        MessagePusher.pushMessage(role, respGeneralNotify);
        //改成使用物品弹出对话。直接进入战斗
        SpringUtils.getBean(YhuaNianshouBossHandler.class).breakSeal_Nianshou(role, type);
    }
}
